//
//  WeatherTipView.m
//  Project - B
//
//  Created by lcy on 15/10/20.
//  Copyright (c) 2015年 lcy. All rights reserved.
//

#import "WeatherTipView.h"
#import "PKRequestManager.h"
#import "TodayTemperatureModel.h"
#import "FutureTemperatureModel.h"
#import "CurrentWeatherModel.h"
#import "UIColor+ThemeColor.h"
@implementation WeatherTipView

// 初始化frame
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView.layer.cornerRadius = 5;
        self.backgroundView.backgroundColor = [UIColor colorWithWeather];
        [self addSubview:self.backgroundView];
        
        UILabel *city = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 30)];
        city.text = @"城市";
        [self.backgroundView addSubview:city];
        
        self.cityTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 10, 80, 30)];
        self.cityTextField.borderStyle = UITextBorderStyleRoundedRect;
        [self.backgroundView addSubview:self.cityTextField];
        
        UIButton *searchWeather = [UIButton buttonWithType:UIButtonTypeCustom];
        [searchWeather setTitle:@"search" forState:UIControlStateNormal];
        [searchWeather setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        searchWeather.frame = CGRectMake(180, 10, 60, 30);
        [searchWeather addTarget:self action:@selector(searchWeather:) forControlEvents:UIControlEventTouchUpInside];
        [self.backgroundView addSubview:searchWeather];
        
        
        self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 260, 30)];
        self.dateLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroundView addSubview:self.dateLabel];
        
        self.temperature = [[UILabel alloc] initWithFrame:CGRectMake(10, 110, 120, 30)];
        [self.backgroundView addSubview:self.temperature];
        
        self.locatedCity = [[UILabel alloc] initWithFrame:CGRectMake(130, 110, 120, 30)];
        [self.backgroundView addSubview:self.locatedCity];
        
        self.weather = [[UILabel alloc] initWithFrame:CGRectMake(10, 160, 120, 30)];
        [self.backgroundView addSubview:self.weather];
        
        self.uv_index = [[UILabel alloc] initWithFrame:CGRectMake(130, 160, 120, 30)];
        [self.backgroundView addSubview:self.uv_index];
        
        self.currentTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 210, 260, 30)];
        self.currentTimeLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroundView addSubview:self.currentTimeLabel];
        
        self.currentTemp = [[UILabel alloc] initWithFrame:CGRectMake(10, 260, 120, 30)];
        [self.backgroundView addSubview:self.currentTemp];
        
        self.humidity = [[UILabel alloc] initWithFrame:CGRectMake(130, 260, 120, 30)];
        [self.backgroundView addSubview:self.humidity];
    }
    return self;
}


- (NSMutableArray *)futureWeather
{
    if(_futureWeather == nil)
    {
        self.futureWeather = [NSMutableArray array];
    }
    return _futureWeather;
}

// 搜索天气
- (void)searchWeather:(UIButton *)btn
{
    [self.cityTextField resignFirstResponder];
    NSString *path = @"http://apis.haoservice.com/weather";
    
    NSDictionary *dict;
    if(self.cityTextField.text.length > 0 && ![self.cityTextField.text hasSuffix:@"省"] && ![self.cityTextField.text hasSuffix:@"市"])
    {
        dict = @{@"key":@"d07dec45b3944316a6d2f2961bcce010", @"cityname":self.cityTextField.text};
    }else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"请输入城市名称（不含省、市字样）" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    [PKRequestManager requestWithType:RequestTypePost urlStr:path parDic:dict finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@", dic);
        
        // 今日
        TodayTemperatureModel *todayModel = [[TodayTemperatureModel alloc] init];
        [todayModel setValuesForKeysWithDictionary:dic[@"result"][@"today"]];
        
        self.dateLabel.text = todayModel.date_y;
        
        self.temperature.text = [NSString stringWithFormat:@"温度：%@", todayModel.temperature];
        
        if(self.cityTextField.text.length == 0)
        {
            self.locatedCity.text = @"当前城市：上海";
        }else
        {
            self.locatedCity.text = [NSString stringWithFormat:@"当前城市：%@", self.cityTextField.text];
        }
        
        self.weather.text = [NSString stringWithFormat:@"天气：%@", todayModel.weather];
        
        self.uv_index.text = [NSString stringWithFormat:@"紫外线：%@", todayModel.uv_index];
        
        
        // 当前
        CurrentWeatherModel *currentModel = [[CurrentWeatherModel alloc] init];
        [currentModel setValuesForKeysWithDictionary:dic[@"result"][@"sk"]];
        
        self.currentTimeLabel.text = currentModel.time;
        
        self.currentTemp.text = [NSString stringWithFormat:@"当前温度：%@", currentModel.temp];
        
        self.humidity.text = [NSString stringWithFormat:@"污染指数：%@", currentModel.humidity];
        
        
        // 未来天气
        for (NSDictionary *diction in dic[@"result"][@"future"]) {
            FutureTemperatureModel *futureModel = [[FutureTemperatureModel alloc] init];
            [futureModel setValuesForKeysWithDictionary:diction];
            [self.futureWeather addObject:futureModel];
        }
        
    } error:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}


@end
